package org.yty.travelagencyredis.Dao.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.yty.travelagencyredis.Dao.TourGroupDao;
import org.yty.travelagencyredis.model.Entity.TourGroup;

import java.util.Collections;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class TourGroupDaoImpl implements TourGroupDao {
    private final StringRedisTemplate redisTemplate;

    @Override
    public TourGroup selectById(Long id) {
        String tourGroupJson = (String) this.redisTemplate.opsForHash().get(TourGroup.key, id);
        return JSON.parseObject(tourGroupJson, TourGroup.class);
    }

    @Override
    public boolean insert(TourGroup tourGroup) {
        this.redisTemplate.opsForHash().put(TourGroup.key, tourGroup.getId(), JSON.toJSONString(tourGroup));
        return true;
    }

    @Override
    public boolean updateById(TourGroup tourGroup) {
        this.redisTemplate.opsForHash().put(TourGroup.key, tourGroup.getId(), JSON.toJSONString(tourGroup));
        return true;
    }

    @Override
    public void deleteById(Long id) {
        this.redisTemplate.opsForHash().delete(TourGroup.key, id);
    }

    @Override
    public List<TourGroup> getListByIdList(List<Long> tourGroupIdList) {
        List<Object> list = this.redisTemplate.opsForHash().multiGet(TourGroup.key, Collections.singleton(tourGroupIdList));
        return list.stream().map(item -> JSON.parseObject((String) item, TourGroup.class)).toList();
    }
}
